package com.huyn.baseframework.statesync;

import android.content.Context;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.huyn.baseframework.model.BaseModel;
import com.huyn.baseframework.statesync.model.ActorState;
import com.huyn.baseframework.utils.Constant;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.UserUtil;

import java.util.HashMap;

public class ActorStateAsyncHelper extends BaseAsyncHelper<ActorState> {

    private static volatile ActorStateAsyncHelper instance = null;

    private ActorStateAsyncHelper(Context context, INetworkRequestBridge bridge) {
        super(context, bridge);
    }

    public static ActorStateAsyncHelper getInstance(Context context, INetworkRequestBridge bridge) {
        if (instance == null) {
            synchronized(ActorStateAsyncHelper.class) {
                if (instance == null) {
                    instance = new ActorStateAsyncHelper(context.getApplicationContext(), bridge);
                }
            }
        }
        return instance;
    }

    @Override
    protected void doRequest(ActorState state) {
//        EventBus.getDefault().post(state);
//        EventBus.getDefault().post(new EventDeliverModel(EventDeliverModel.ACTOR, state));

        HashMap<String, String> maps = new HashMap<>();
        maps.put(Constant.API_REQUEST_METHOD, "com.aube.actor.praise");
        maps.put("relatedId", state.id);
        if(iNetworkRequestBridge != null) {
            iNetworkRequestBridge.executeRequest(maps, BaseModel.class, null, new Response.Listener<BaseModel>() {
                @Override
                public void onResponse(BaseModel response) {

                }

                @Override
                public void onErrorResponse(VolleyError error) {

                }

                @Override
                public void onStart() {

                }
            });
        }
    }

    public int getFavorCount(String id, int count, boolean state) {
        if(objMaps.containsKey(id))
            return objMaps.get(id).favorNum;
        else
            objMaps.put(id, new ActorState(id, state, -1, count));
        return count;
    }

    public boolean isFavored(String id, int count, boolean state) {
        if(!UserUtil.isUserLogin())
            return false;
        if(objMaps.containsKey(id))
            return objMaps.get(id).favored;
        else
            objMaps.put(id, new ActorState(id, state, -1, count));
        return state;
    }

    public void updateactor(String id, int count, boolean favor) {
        if(objMaps.containsKey(id)) {
            ActorState state = objMaps.get(id);

            if(state.favorNum == 0) {
                state.updateState(favor);
                int collectedTimes = 0;
                try {
                    collectedTimes = count;
                } catch (Exception e) {
                }
                state.updateFavorNum(collectedTimes);
//                EventBus.getDefault().post(state);
//                EventBus.getDefault().post(new EventDeliverModel(EventDeliverModel.ACTOR, state));
            }
        } else {
            objMaps.put(id, new ActorState(id, favor, -1, count));
        }

        notifyDataChanged();
    }

    public void updateactorWithoutNotify(String id, int count, boolean favor) {
        if(objMaps.containsKey(id)) {
            ActorState state = objMaps.get(id);

            if(state.favorNum == 0) {
                state.updateState(favor);
                int collectedTimes = 0;
                try {
                    collectedTimes = count;
                } catch (Exception e) {
                }
                state.updateFavorNum(collectedTimes);
//                EventBus.getDefault().post(state);
//                EventBus.getDefault().post(new EventDeliverModel(EventDeliverModel.ACTOR, state));
            }
        } else {
            objMaps.put(id, new ActorState(id, favor, -1, count));
        }
    }

    public synchronized ActorState operate(String id) {
        if(StringUtils.isBlank(id))
            return null;
        ActorState state;
        if(objMaps.containsKey(id)) {
            state = objMaps.get(id);
            state.changeState();
        } else {
            return null;
        }

        int pid = Integer.parseInt(id);

        mHandler.removeMessages(pid);
        mHandler.sendEmptyMessageDelayed(pid, STATE_DELAY_TIME);

        notifyDataChanged();

        return state;
    }

}
